package com.wnxy.admin.task;

import com.wnxy.admin.common.util.AliOSSUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ImageTask {
    private final RedisTemplate redisTemplate;
    private final AliOSSUtils aliOSSUtils;

    public ImageTask(RedisTemplate redisTemplate, AliOSSUtils aliOSSUtils) {
        this.redisTemplate = redisTemplate;
        this.aliOSSUtils = aliOSSUtils;
    }

    @Scheduled(cron = "0 0/10 * * * ? ")
    public void clearImage() throws Exception {
        System.out.println("===============开始删除redis图片==============");
        Set<String> difference = redisTemplate.opsForSet().difference("upload-file-all", "upload-file-db");

        if (difference != null && difference.size() > 0) {
            for (String fileName : difference) {
                aliOSSUtils.delete(fileName);
                //删除redis的无效图片
                redisTemplate.opsForSet().remove("upload-file-all", fileName);
            }
        }
    }
}
